package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        //新增检查组操作
        checkGroupDao.add(checkGroup);
        //获取新增的id值
        Integer checkGroupId = checkGroup.getId();
        //封装id值
        HashMap<String,Integer> mp = new HashMap<String,Integer>();

            mp.put("checkGroupId",checkGroupId);

            if(checkItemIds != null && checkItemIds.length>0){
                for (Integer checkItemId : checkItemIds) {
                    mp.put("checkItemId",checkItemId);
                    checkGroupDao.setCheckGroupAndCheckItem(mp);
                }

            }
    }
    //分页查询所有
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        System.out.println("CheckGroupServiceImpl  ============= queryPageBean :" + queryPageBean);
        //取出数据queryPageBean中的数据
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页此案时的条数
        String queryString = queryPageBean.getQueryString();//条件查询
        //分页查询
        PageHelper.startPage(currentPage,pageSize);
        //根据条件查询c
        Page<CheckGroup> page = checkGroupDao.findByCondItion(queryString);
        //当前页结果
        List<CheckGroup> rows = page.getResult();
        //总页数
        long total = page.getTotal();
        return new PageResult(total,rows);
    }

    //根据id查找检查组
    @Override
    public CheckGroup findById(Integer id) {

        CheckGroup checkGroup = checkGroupDao.findById(id);

        return checkGroup;

    }
    //查询中间表id集合
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        System.out.println("id = ============================== " + id);
       return checkGroupDao.findCheckItemIdsByCheckGroupId(id);

    }

    //编辑检查组
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {

        //更新编辑检查组信息
        checkGroupDao.updateCheckGroup(checkGroup);

        //删除中间表的id值
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleteAssoication(checkGroupId);

        //添加中间表关联id
        if(checkitemIds != null && checkitemIds.length > 0){

            for (Integer checkitemId : checkitemIds) {
                //将值存入Map集合
                Map<String, Integer> map = new HashMap<String, Integer>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);

                checkGroupDao.setCheckGroupAndCheckItems(map);
            }
        }
    }

    //查询所有用户
    @Override
    public List<CheckGroup> findAll() {

         return checkGroupDao.findAll();

    }

}
